package cn.kent.receiver;

import cn.kent.config.RabbitConfig;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class MsgReceiver {

    @RabbitListener(queues = RabbitConfig.QUEUE_NAME)
    public void handlerMsg(Message message, Channel channel) throws IOException {
        System.out.println("###  receive = " + message.getPayload());
        // 确认消息
        channel.basicAck((Long) message.getHeaders().get(AmqpHeaders.DELIVERY_TAG), true);
//        System.out.println("msg = " + msg);
    }

    /**
     * concurrency 指的是并发数，即 将开启20个子线程去消费
     */
    @RabbitListener(queues = RabbitConfig.QUEUE_NAME, concurrency = "10")
    public void handlerMsg2(Message message, Channel channel) throws IOException {
//        System.out.println("reject = " + message.getPayload());
        // 拒绝消费该消费，requeue表示被拒绝的消息是否重新进入队列中
        channel.basicReject((Long) message.getHeaders().get(AmqpHeaders.DELIVERY_TAG), true);
    }

}
